package com.cy.aircompressor.main.ui.onlinemonitor.fragment.child;

import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.cy.aircompressor.databinding.ChildContainerSecondFragmentBinding;
import com.cy.aircompressor.entity.select.SelectDict;
import com.cy.aircompressor.entity.sensor.Sensor;
import com.cy.aircompressor.entity.sensor.SensorStatisticsData;
import com.cy.aircompressor.entity.sensor.VibrationDataChangeTrend;
import com.cy.aircompressor.main.ui.onlinemonitor.fragment.OnlineMonitorFragment;
import com.cy.aircompressor.main.ui.onlinemonitor.provider.VibrationChangeTrendProvider;
import com.cy.aircompressor.main.ui.onlinemonitor.viewmodel.OnlineMonitorViewModel;
import com.drug.lib.view.recycptr.DynamicAdpTypePool;
import com.drug.lib.view.recycptr.DynamicRecyclerAdapter;
import com.drug.lib.view.recycptr.bean.RowEntity;
import com.net.model.base.Resource;
import com.net.model.base.fragment.BaseVMFragment;
import com.net.model.livedatas.LiveDataBus;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class VibrationChangeFragment extends BaseVMFragment<ChildContainerSecondFragmentBinding, OnlineMonitorViewModel> {

    public static VibrationChangeFragment newInstance() {
        Bundle args = new Bundle();
        VibrationChangeFragment fragment = new VibrationChangeFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void dataChange() {
        LiveDataBus.get(VibrationChangeFragment.class.getSimpleName(), SelectDict.class).observe(this, new Observer<SelectDict>() {
            @Override
            public void onChanged(SelectDict selectDict) {
                requestNetwork();
            }

        });

        mViewModel.getQuerySensorByPageObservable().observe(this, new Observer<Resource<RowEntity<Sensor>>>() {
            @Override
            public void onChanged(Resource<RowEntity<Sensor>> rowEntityResource) {
                rowEntityResource.handler(new OnCallback<RowEntity<Sensor>>() {
                    @Override
                    public void onSuccess(RowEntity<Sensor> data) {
                        if (data.getList() == null) return;
                        List<VibrationDataChangeTrend> changeTrends = filterParts("body", data.getList());
                        dynamicRecyclerAdapter.setItems(changeTrends);
                        dynamicRecyclerAdapter.notifyDataSetChanged();

                    }
                });
            }
        });

        mViewModel.getQuerySensorStatisticsDataDataObservable().observe(this, new Observer<Resource<List<SensorStatisticsData>>>() {
            @Override
            public void onChanged(Resource<List<SensorStatisticsData>> listResource) {
                listResource.handler(new OnCallback<List<SensorStatisticsData>>() {
                    @Override
                    public void onSuccess(List<SensorStatisticsData> data) {
                        if (data != null && data.size() > 0) {
                            List<VibrationDataChangeTrend> changeTrends = dynamicRecyclerAdapter.getItems();
                            int changeIndex = -1;
                            SensorStatisticsData sensorStatisticsData = data.get(0);
                            VibrationDataChangeTrend vibrationDataChangeTrend = null;
                            for (VibrationDataChangeTrend changeTrend : changeTrends) {
                                if (TextUtils.equals(changeTrend.getVibrationSensor().getSn(), sensorStatisticsData.getSn())) {
                                    vibrationDataChangeTrend = changeTrend;
                                    changeIndex = changeTrends.indexOf(changeTrend);
                                }
                            }
                            List<VibrationDataChangeTrend.VibrationData> xrmsList = new ArrayList<>();
                            List<VibrationDataChangeTrend.VibrationData> xspeedList = new ArrayList<>();
                            List<VibrationDataChangeTrend.VibrationData> yrmsList = new ArrayList<>();
                            List<VibrationDataChangeTrend.VibrationData> yspeedList = new ArrayList<>();
                            List<VibrationDataChangeTrend.VibrationData> zrmsList = new ArrayList<>();
                            List<VibrationDataChangeTrend.VibrationData> zspeedList = new ArrayList<>();
                            for (SensorStatisticsData statisticsData : data) {
                                VibrationDataChangeTrend.VibrationData vibrationData = new VibrationDataChangeTrend.VibrationData();
                                vibrationData.setValue(statisticsData.getXspeed());
                                vibrationData.setDate(statisticsData.getRt());
                                xspeedList.add(vibrationData);

                                vibrationData = new VibrationDataChangeTrend.VibrationData();
                                vibrationData.setValue(statisticsData.getXrms());
                                vibrationData.setDate(statisticsData.getRt());
                                xrmsList.add(vibrationData);

                                vibrationData = new VibrationDataChangeTrend.VibrationData();
                                vibrationData.setValue(statisticsData.getYspeed());
                                vibrationData.setDate(statisticsData.getRt());
                                yspeedList.add(vibrationData);

                                vibrationData = new VibrationDataChangeTrend.VibrationData();
                                vibrationData.setValue(statisticsData.getYrms());
                                vibrationData.setDate(statisticsData.getRt());
                                yrmsList.add(vibrationData);

                                vibrationData = new VibrationDataChangeTrend.VibrationData();
                                vibrationData.setValue(statisticsData.getXspeed());
                                vibrationData.setDate(statisticsData.getRt());
                                zspeedList.add(vibrationData);

                                vibrationData = new VibrationDataChangeTrend.VibrationData();
                                vibrationData.setValue(statisticsData.getZrms());
                                vibrationData.setDate(statisticsData.getRt());
                                zrmsList.add(vibrationData);


                            }
                            vibrationDataChangeTrend.setXrms(xrmsList);
                            vibrationDataChangeTrend.setXspeed(xspeedList);
                            vibrationDataChangeTrend.setYrms(yrmsList);
                            vibrationDataChangeTrend.setYspeed(yspeedList);
                            vibrationDataChangeTrend.setZrms(zrmsList);
                            vibrationDataChangeTrend.setZspeed(zspeedList);

                            if (changeIndex != -1)
                                dynamicRecyclerAdapter.notifyItemChanged(changeIndex);
                        }
                    }
                });
            }
        });
    }

    @Override
    protected ChildContainerSecondFragmentBinding getViewBinding() {
        return ChildContainerSecondFragmentBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initData() {
        initSensorDataRecycler();
    }

    @Override
    protected void requestNetwork() {
        mViewModel.querySensorByPage(OnlineMonitorFragment.selectDict.getValue());
    }

    DynamicRecyclerAdapter dynamicRecyclerAdapter;

    private void initSensorDataRecycler() {
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(mContext);
        mBinding.recyclerView.setLayoutManager(mLayoutManager);
        dynamicRecyclerAdapter = new DynamicRecyclerAdapter();
        DynamicAdpTypePool pool = new DynamicAdpTypePool();
        VibrationChangeTrendProvider vibrationChangeTrendProvider = new VibrationChangeTrendProvider(mContext);
        pool.register(VibrationDataChangeTrend.class, vibrationChangeTrendProvider);
        dynamicRecyclerAdapter.registerAll(pool);
        mBinding.recyclerView.setAdapter(dynamicRecyclerAdapter);
    }


    private List<VibrationDataChangeTrend> filterParts(String partCode, List<Sensor> sensors) {
        List<Sensor> result = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            result = sensors.stream()
                    .filter(new Predicate<Sensor>() {
                        @Override
                        public boolean test(Sensor sensor) {
                            return TextUtils.equals(sensor.getCompressorPartCode(), partCode)
                                    && sensor.getCompressorPointCode().contains("vibration");
                        }
                    }).collect(Collectors.toList());
        }
        List<VibrationDataChangeTrend> changeTrends = new ArrayList<>();
        if (result != null && result.size() != 0) {

            for (Sensor sensor : result) {
                if (sensor == null) continue;
                VibrationDataChangeTrend vibrationDataChangeTrend = new VibrationDataChangeTrend();
                vibrationDataChangeTrend.setVibrationSensor(sensor);
                vibrationDataChangeTrend.setPartName(sensor.getName());
                changeTrends.add(vibrationDataChangeTrend);
                mViewModel.querySensorStatisticsData("2024-05-23 17:27:50", "2024-10-03 17:27:50", sensor.getSn(), Integer.parseInt(sensor.getType()));
            }
            return changeTrends;
        }
        return null;
    }
}
